package com.usian.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.usian.commom.pojo.User;

import java.util.List;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2023 11 22 09 30
 **/
@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {


    @Autowired
    UserService userService;

    /**
     * 查询所有的用户列表
     * Get
     *  @RequestMapping(value = "/findAllUser",method = RequestMethod.GET)
     *     @GetMapping("/findAllUser")
     * @return
     */
    @GetMapping("/findAllUser")
    public List<User> findAllUser(){

        log.info("9080---------------------");
        List<User> list = userService.list();
        return list;
    }


    /**
     * j根据用户ID查询用户
     */

    @GetMapping("/findUserById")
    public User findUserById(@RequestParam Long id){
        User user = userService.getById(id);
        return user;
    }

    /**
     * {id}:占位符
     * /findUserByUserId/2
     * @param id
     * @return
     */
    @GetMapping("/findUserByUserId/{id}")
    public User findUserByUserId(@PathVariable Long id){
        User user = userService.getById(id);
        return user;
    }

    /**
     *
     * @param username
     * @return
     */
    @GetMapping("/findUser")
    public List<User> findUser(String username){

        List<User> user = userService.findUser(username);
        return user;

    }







    @GetMapping("/findUserByUsernameANdage")
    public List<User> findUserByUsernameANdage(String username,Integer age){

        List<User> userByUsernameANdage = userService.findUserByUsernameANdage(username, age);
        return userByUsernameANdage;

    }

    @PostMapping("/findUserByUser")
    public List<User> findUserByUser(@RequestBody User user){

        // System.out.println("user:"+user.Tostring());
        log.info("user:{}",user.toString());

        List<User> userByUser = userService.findUserByUser(user);
        return userByUser;
    }


    @PostMapping("/findUserPage")
    public Page<User> findUserPage(@RequestBody User user
            , @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum
            , @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){

        Page<User> userPage = userService.findUserPage(user, pageNum, pageSize);
        return userPage;
    }


    @PostMapping("/saveUser")
    public Boolean saveUser(@RequestBody User user){
        boolean save = userService.save(user);
        return save;
    }

    @PostMapping("/updataUser")
    public Boolean updataUser(@RequestBody User user){
        boolean b = userService.updateById(user);
        return b;
    }



    @GetMapping("/delUser/{id}")
    public Boolean delUser(@PathVariable Long id){
        boolean b = userService.removeById(id);
        return b;
    }

}
